//
//  MYStatusCell.m
//  MYChatting
//
//  Created by mac on 16/8/20.
//  Copyright © 2016年 lhf. All rights reserved.
//

#import "MYStatusCell.h"
#import "Status.h"
#import "MYStatusCellTopView.h"
#import "MYStatusCellBottomView.h"
#import "Masonry.h"
#import "MYStatusRetweetedView.h"

@interface MYStatusCell ()

@property (nonatomic,weak) MYStatusCellTopView *topView;
@property (nonatomic,weak) MYStatusCellBottomView *bottomView;
@property (nonatomic,weak) MYStatusRetweetedView *retweetedView;

//设置bottomView的顶部约束
@property (nonatomic,weak) MASConstraint *bottomViewTopConstraint;

@end

@implementation MYStatusCell



- (nonnull instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier{
   self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI{
    MYStatusCellTopView *topView = [[MYStatusCellTopView alloc] init];
    self.topView = topView;
    [self.contentView addSubview:self.topView];
    
    MYStatusRetweetedView *retweetedView = [[MYStatusRetweetedView alloc] init];
    self.retweetedView = retweetedView;
    [self.contentView addSubview:self.retweetedView];
    
    MYStatusCellBottomView *bottomView = [[MYStatusCellBottomView alloc] init];
    self.bottomView = bottomView;
    [self.contentView addSubview:self.bottomView];
    
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.contentView);
        //便于测试
        //make.height.mas_equalTo(50);
    }];
    
    [self.retweetedView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentView);
        make.top.equalTo(self.topView.mas_bottom);
    }];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
      self.bottomViewTopConstraint =  make.top.equalTo(self.retweetedView.mas_bottom);
        make.left.right.equalTo(self.contentView);
        //便于测试
        make.height.mas_equalTo(40);
    }];
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
        make.bottom.equalTo(self.bottomView.mas_bottom);
    }];
    
}

- (void)setStatus:(Status *)status{
    _status = status;
    
    self.topView.status = status;
    //NSLog(@"retweeted_status------%@",status.retweeted_status);
    
    //卸载bottomView的顶部约束  重新设置
    [self.bottomViewTopConstraint uninstall];
    
    if (status.retweeted_status != nil ) {
        [self.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
            self.bottomViewTopConstraint = make.top.equalTo(self.retweetedView.mas_bottom);
        }];
        self.retweetedView.hidden = NO;
        self.retweetedView.retweetedStatus = status.retweeted_status;
    }else{
        [self.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
            self.bottomViewTopConstraint = make.top.equalTo(self.topView.mas_bottom);
        }];
        self.retweetedView.hidden = YES;
    }
}




@end
